﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace DsLib
{
    public enum WordCompareType
    {
        Length,
        Lexical
    }

    public class EnglishDictionaryWord : IComparer<EnglishDictionaryWord>, IComparable<EnglishDictionaryWord>
    {
        WordCompareType _compareType;
        IComparer<EnglishDictionaryWord> _comparer = null;

        public int Length
        {
            get;
            internal protected set;
        }

        public string Word
        {
            get;
            internal protected set;
        }

        public EnglishDictionaryWord(string word, IComparer<EnglishDictionaryWord> comparer)
        {
            Word = word;
            Length = Word.Length;
            _comparer = comparer;
        }

        public EnglishDictionaryWord(string word, WordCompareType type)
        {
            Word = word;
            Length = Word.Length;
            _compareType = type;

            if (_compareType == WordCompareType.Length)
                _comparer = new WordLengthComparer();
            else if (_compareType == WordCompareType.Lexical)
                _comparer = new WordLexicalComparer();
            else
                throw new InvalidOperationException("Unsupported Comparison type");
        }

        public EnglishDictionaryWord(string word) : this(word, WordCompareType.Lexical)
        {

        }

        public int Compare(EnglishDictionaryWord x, EnglishDictionaryWord y)
        {
            return _comparer.Compare(x, y);
        }

        public int CompareTo(EnglishDictionaryWord obj)
        {
            return Compare(this, obj);
        }

        public override string ToString()
        {
            return Word;
        }
    }

    public class WordLengthComparer : IComparer<EnglishDictionaryWord>
    {
        public WordLengthComparer()
        {
            
        }
        public int Compare(EnglishDictionaryWord x, EnglishDictionaryWord y)
        {
            int result = x.Length - y.Length;

            if (result == 0)
            {
                result = string.CompareOrdinal(x.Word, y.Word);
            }

            return result;
        }
    }

    public class WordLexicalComparer : IComparer<EnglishDictionaryWord>
    {
        public WordLexicalComparer()
        {
            
        }
        public int Compare(EnglishDictionaryWord x, EnglishDictionaryWord y)
        {
            return string.CompareOrdinal(x.Word, y.Word);
        }
    }
}
